#include "ccd290_extract.h"

#define size_tmp  22

void make_fake_ccd_data(char fname[], ccd290_config* cfg) {

//	prepare fake CCD image date
    uint16_t *ccd_image = (uint16_t*)malloc(sizeof(uint16_t)*cfg->LEN*4);
    
    uint16_t m,n;
    for( m=0; m<2*cfg->N_ROW; m++ ) {
        for( n=0; n<2*cfg->N_COL; n++ ) {
            ccd_image[m*2*cfg->N_COL+n] = m + n;
        }
    }

/*	FILE *fp_ref = fopen("fake_ccd.txt","w");*/
/*	for(m=0; m<2*N_ROW; m++){*/
/*		for(n=0; n<2*N_COL; n++){*/
/*			fprintf(fp_ref,"%6d ",ccd_image[m*2*N_COL+n]);*/
/*		}*/
/*		fprintf(fp_ref,"\n");*/
/*	}*/
/*	fclose(fp_ref);*/
    
    FILE* fp = fopen(fname,"wb");
    uint i,j,k;
    for(i=0; i<cfg->N_ROW; i+=1) {
        for(j=0; j<cfg->N_COL; j++) {

            uint16_t E = (i+j);
            uint16_t F = (i+j);
            uint16_t G = (i+j);
            uint16_t H = (i+j);

            uint idx_E = i*2*cfg->N_COL + j;
            uint idx_F = i*2*cfg->N_COL + (2*cfg->N_COL-j-1);
            uint idx_G = (2*cfg->N_ROW-i-1)*2*cfg->N_COL + j;
            uint idx_H = (2*cfg->N_ROW-i-1)*2*cfg->N_COL + (2*cfg->N_COL-j-1);
            
            uint16_t tmpE[size_tmp]={0}, tmpF[size_tmp]={0}, tmpG[size_tmp]={0}, tmpH[size_tmp]={0};

            for(k=0; k<size_tmp; k++) {
                if( k < 10 ) {
                    tmpE[k] = E + ccd_image[idx_E];
                    tmpF[k] = F + ccd_image[idx_F];
                    tmpG[k] = G + ccd_image[idx_G];
                    tmpH[k] = H + ccd_image[idx_H];
                } else {
                    tmpE[k] = E;
                    tmpF[k] = F;
                    tmpG[k] = G;
                    tmpH[k] = H;
                }
            }

            for(k=0; k<size_tmp-1; k++) {

                //	add 4-bit shift (Xs: 's' means shifted)
                uint16_t Es = ( (tmpE[k] & 0x000f) << 12) + ( (tmpE[k+1] & 0xfff0) >> 4);
                uint16_t Fs = ( (tmpF[k] & 0x000f) << 12) + ( (tmpF[k+1] & 0xfff0) >> 4);
                uint16_t Gs = ( (tmpG[k] & 0x000f) << 12) + ( (tmpG[k+1] & 0xfff0) >> 4);
                uint16_t Hs = ( (tmpH[k] & 0x000f) << 12) + ( (tmpH[k+1] & 0xfff0) >> 4);

/*				uint16_t Es = (tmpE[k+1] & 0x000f << 12) + (tmpE[k] & 0xfff0 >> 4);*/
/*				uint16_t Fs = (tmpF[k+1] & 0x000f << 12) + (tmpF[k] & 0xfff0 >> 4);*/
/*				uint16_t Gs = (tmpG[k+1] & 0x000f << 12) + (tmpG[k] & 0xfff0 >> 4);*/
/*				uint16_t Hs = (tmpH[k+1] & 0x000f << 12) + (tmpH[k] & 0xfff0 >> 4);*/

                
                uint32_t EF = (Es << 16) | Fs;
                uint32_t GH	= (Gs << 16) | Hs;

/*                swap_endian16(&Es);*/
/*                swap_endian16(&Fs);*/
/*                swap_endian16(&Gs);*/
/*                swap_endian16(&Hs);*/
/*                uint32_t EF = (Es << 0) | (Fs<<16);*/
/*                uint32_t GH	= (Gs << 0) | (Hs<<16);*/

            //  ��顰shift bit���Ƿ���ȷ
/*                if((k==0 || k==1) && i==1){*/
/*		            printf("I = %5d, J = %5d K = %d\t tmpG[k] = 0x%04x Gs = 0x%04x; tmpH[k] = 0x%04x Hs = 0x%04x \t", i, j, k, tmpG[k], Gs, tmpH[k], Hs);*/
/*                    printf("GH = 0x%08x |\t", GH);*/
/*                }*/
                
            //  �˴������Ƿ���Ҫת����С�ˣ���Ҫ���Arm Linux SoC��ʵ����������ڱ�����Ĳ��Զ��ԣ�����Ҫת��
/*                swap_endian32(&EF);*/
/*                swap_endian32(&GH);*/
                
/*                if((k==0 || k==1) && i==1){*/
/*                    printf("sGH = 0x%08x\n", GH);*/
/*                }*/
                
                //	no 4-bit shift
/*				uint EF = (tmpE[k] << 16) + tmpF[k];*/
/*				uint GH	= (tmpG[k] << 16) + tmpH[k];*/

/*                fwrite(&EF, sizeof(unsigned char), 4, fp);*/
/*                fwrite(&GH, sizeof(unsigned char), 4, fp);*/

                fwrite(&EF, sizeof(uint32_t), 1, fp);
                fwrite(&GH, sizeof(uint32_t), 1, fp);
            }
			
/*			if( i==1 && j > 5) exit(0);*/
        }
    }
    
    fclose(fp);
    free(ccd_image);
}
